package com.shopnight.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shopnight.config.WxPayConfig;
import com.shopnight.constant.ShopIndentConstant;
import com.shopnight.entity.AllOrder;
import com.shopnight.entity.ErrandOrder;
import com.shopnight.entity.ShopGoods;
import com.shopnight.entity.ShopIndent;
import com.shopnight.mapper.AllOrderMapper;
import com.shopnight.mapper.ErrandOrderMapper;
import com.shopnight.mapper.ShopGoodsMapper;
import com.shopnight.mapper.ShopIndentMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisKeyExpirationListenerUtil extends KeyExpirationEventMessageListener {

    @Autowired
    ShopIndentMapper shopIndentMapper;
    @Autowired
    ErrandOrderMapper errandOrderMapper;
    @Autowired
    ShopGoodsMapper shopGoodsMapper;
    @Autowired
    AllOrderMapper allOrderMapper;
    @Autowired
    private AllOrderUtil allOrderUtil;
    @Autowired
    WxPayConfig wxPayConfig;
    @Autowired
    WxMiniPayUtils wxMiniPayUtils;


        public RedisKeyExpirationListenerUtil(RedisMessageListenerContainer listenerContainer) {
            super(listenerContainer);
        }

        /**
         * 使用该方法监听，当我们key过期是自动调用该方法
         * @param message
         * @param pattern
         */
        @SneakyThrows
        @Override
        public void onMessage(Message message, byte[] pattern) {
            String s = message.toString();//就是过期的key
            String[] split = s.split(":");
            //网超零食订单过期
            if (split[0].equals("snack-order")){
                String code = split[1];
                ShopIndent shopIndent = shopIndentMapper.selectOne(new LambdaQueryWrapper<ShopIndent>().eq(ShopIndent::getIndentCode, code));
                if (shopIndent.getIndentStatus() == ShopIndentConstant.StatusEnum.unpaid.getStatus()){
                    // 更改腾讯支付订单的状态
                    WxMiniPayUtils.closeOrder(code);
                    shopIndent.setIndentStatus(ShopIndentConstant.StatusEnum.cancel.getStatus());
                    //更改订单状态为取消
                    shopIndentMapper.updateById(shopIndent);

                    //还原库存
                    List<ShopGoods> shopGoodsList = shopGoodsMapper.selectList(
                            new LambdaQueryWrapper<ShopGoods>()
                                    .eq(ShopGoods::getIndentId,shopIndent.getIndentId())
                    );
                    shopGoodsMapper.updateStock(shopGoodsList);

                    //更改顺序订单表状态为取消
                    allOrderUtil.updateAllOrderStatus(Integer.valueOf(shopIndent.getIndentId().toString()),ShopIndentConstant.StatusEnum.cancel.getStatus());
                }
            }

            //校园跑腿订单过期
            if (split[0].equals("errand-order")){
                String code = split[1];
                ErrandOrder errandOrder = errandOrderMapper.selectOne(new LambdaQueryWrapper<ErrandOrder>().eq(ErrandOrder::getCode, code));
                if (errandOrder.getStatus() == ShopIndentConstant.StatusEnum.unpaid.getStatus()){
                    // 更改腾讯支付订单的状态
                    WxMiniPayUtils.closeOrder(code);
                    errandOrder.setStatus(ShopIndentConstant.StatusEnum.cancel.getStatus());
                    //更改订单状态为取消
                    errandOrderMapper.updateById(errandOrder);

                    //更改顺序订单表状态为取消
                    allOrderUtil.updateAllOrderStatus(errandOrder.getId(),ShopIndentConstant.StatusEnum.cancel.getStatus());
                }
            }

        }


}
